﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.AdvTree;
using SyncConfig.Dialog;
using DevComponents.DotNetBar.Controls;
using SyncLib;
using DevComponents.DotNetBar;
using DevComponents.Editors;
using AppCommon.Controls;
using AppCommon;

namespace SyncConfig.Controls
{
    public partial class TableOfScopeInfo : UserControlBase
    {
        private Node _innerNode;
        public new Node CurrentNode
        {
            get
            {
                return _innerNode;
            }
            set
            {
                _innerNode = value;
                if (_innerNode != null)
                    CurrentRow = _innerNode.Tag as SyncConfigDS.TableOfScopeRow;
            }
        }
        public SyncConfigDS.TableOfScopeRow CurrentRow { get; set; }
        public void DataBind()
        {
            errorProvider1.Clear();
            txtTableName.Text = CurrentRow.TableName;
            txtPrimaryKey.Text = CurrentRow.IsPrimaryKeyNull() ? "" : CurrentRow.PrimaryKey;
            txtPreApplyCommand.Text = CurrentRow.IsPreApplyCommandNull() ? "" : CurrentRow.PreApplyCommand;
            txtAfterApplyCommand.Text = CurrentRow.IsAfterApplyCommandNull() ? "" : CurrentRow.AfterApplyCommand;
            txtSolveConflictCommand.Text = CurrentRow.IsSolveConfigCommandNull() ? "" : CurrentRow.SolveConfigCommand;
            cbSolveConflicType.SelectedValue = CurrentRow.IsSolveConfigTypeNull() ? KeeperSyncUtility.Solve_NoneSpecified : CurrentRow.SolveConfigType;
            DataView view = new DataView(FrmMain.MainForm.TreeManipulation.dsSyncConfig.DependencyTable);
            view.RowFilter = "TableID='" + CurrentRow.TableId.ToString() + "'";
            superGridControl1.PrimaryGrid.DataSource = view;
        }
        public TableOfScopeInfo()
        {
            InitializeComponent();
            errorProvider1.SetIconAlignment(txtPrimaryKey, ErrorIconAlignment.MiddleLeft);
            errorProvider1.SetIconAlignment(txtSolveConflictCommand, ErrorIconAlignment.MiddleLeft);
            errorProvider1.SetIconAlignment(cbSolveConflicType, ErrorIconAlignment.MiddleLeft);
            LoadCombobox();
            //errorProvider1.SetIconAlignment(txtPrimaryKey, ErrorIconAlignment.MiddleLeft);
        }
        void LoadCombobox()
        {
            DataTable tblData = new DataTable();
            tblData.Columns.Add("ID");
            tblData.Columns.Add("Des");
            tblData.Rows.Add(KeeperSyncUtility.Solve_NoneSpecified, "Không chọn");
            tblData.Rows.Add(KeeperSyncUtility.Solve_Auto, "Tự động xử lý");
            tblData.Rows.Add(KeeperSyncUtility.Solve_ServerWin, "Máy chủ luôn thắng");
            tblData.Rows.Add(KeeperSyncUtility.Solve_ClientWin, "Máy trạm luôn thắng");
            tblData.Rows.Add(KeeperSyncUtility.Solve_CallProc, "Gọi thủ tục");



            cbSolveConflicType.DisplayMember = "Des";
            cbSolveConflicType.ValueMember = "ID";
            cbSolveConflicType.DataSource = tblData;


        }
        private void labelLinkX1_Click(object sender, EventArgs e)
        {
            BaseDialog frm = new BaseDialog();
            try
            {
                AddDenpendencyTable ctrl = new AddDenpendencyTable();
                ctrl.Dock = DockStyle.Fill;
                frm.Size = new Size(ctrl.Size.Width + 20, ctrl.Size.Height + 60);
                frm.panelEx1.Controls.Add(ctrl);
                Node depenNode = CurrentNode.Nodes.Find(AdvTreeManipulation.DependencyTableName, false)[0];
                ctrl.CurrentNode = depenNode;
                if (frm.ShowDialog() == DialogResult.OK)
                    DataBind();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                frm.Dispose();
            }
        }

        private void labelLinkX3_Click(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            errorProvider1.Tag = null;
            if (string.IsNullOrEmpty(txtPrimaryKey.Text))
            {
                errorProvider1.SetError(txtPrimaryKey, "Bạn chưa chọn khóa chính cho bảng");
                errorProvider1.Tag = 1;
            }
            if (cbSolveConflicType.SelectedItem == null || cbSolveConflicType.SelectedIndex == 0)
            {
                errorProvider1.SetError(cbSolveConflicType, "Bạn chưa chọn cách xử lý xung đột");
                errorProvider1.Tag = 1;
            }
            else
            {
                if (cbSolveConflicType.SelectedValue.ToString() == KeeperSyncUtility.Solve_CallProc)
                {
                    if (string.IsNullOrEmpty(txtSolveConflictCommand.Text))
                    {
                        errorProvider1.SetError(txtSolveConflictCommand, "Chưa chọn thủ tục xử lý xung đột");
                        errorProvider1.Tag = 1;
                    }
                }
            }
            if (errorProvider1.Tag != null)
            {
                MessageBox.Show("Lỗi nhập liệu");
                return;
            }
            CurrentRow.PrimaryKey = txtPrimaryKey.Text;
            CurrentRow.PreApplyCommand = txtPreApplyCommand.Text;
            CurrentRow.AfterApplyCommand = txtAfterApplyCommand.Text;
            CurrentRow.SolveConfigCommand = txtSolveConflictCommand.Text;
            CurrentRow.SolveConfigType = cbSolveConflicType.SelectedValue.ToString();
            FrmMain.MainForm.TreeManipulation.AdapterTableOfScope.Update(CurrentRow);
            MessageBox.Show("Đã lưu dữ liệu thành công");
        }
        void ChoiceCommand(TextBoxX txtHold, string des)
        {
            if (string.IsNullOrEmpty(des))
                if (!string.IsNullOrEmpty(txtHold.WatermarkText))
                    des = txtHold.WatermarkText;
            PickProcedure frm = new PickProcedure();
            try
            {
                frm.CurrentCommand = txtHold.Text;
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    txtHold.Text = string.Format("[{0}].[{1}]", frm.SelectedRow["Schema"], frm.SelectedRow["Name"]);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                frm.Dispose();
            }
        }

        private void txtPrimaryKey_ButtonCustomClick(object sender, EventArgs e)
        {
            ChoicePrimaryColumn frm = null;
            try
            {
                frm = new ChoicePrimaryColumn();
                frm.TableName = CurrentRow.TableName;
                frm.OldPrimaryColumn = CurrentRow.PrimaryKey;
                frm.ShowDialog();
                if (frm.DialogResult == DialogResult.OK)
                    txtPrimaryKey.Text = frm.CurrentPrimaryColumn;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (frm != null)
                    frm.Dispose();
            }

        }

        private void txtPreApplyCommand_ButtonCustomClick(object sender, EventArgs e)
        {
            ChoiceCommand(txtPreApplyCommand, "Choice PreApply Command");
        }

        private void txtAfterApplyCommand_ButtonCustomClick(object sender, EventArgs e)
        {
            ChoiceCommand(txtAfterApplyCommand, "Choice After Apply Command");
        }

        private void txtSolveConflictCommand_CausesValidationChanged(object sender, EventArgs e)
        {
            ChoiceCommand(txtSolveConflictCommand, "Choice Solve Conflict Command");
        }

        private void labelLinkX2_Click(object sender, EventArgs e)
        {
            if (superGridControl1.PrimaryGrid.ActiveRow != null)
                if (MessageBox.Show("Bạn có chắc muốn xóa dòng đang chọn?", "Xac nhan", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    SyncConfigDS.DependencyTableRow row = superGridControl1.GetCurrentRow<SyncConfigDS.DependencyTableRow>();
                    Node[] result = CurrentNode.Nodes.Find(row.TableName, true);
                    if (result.Length > 0)
                        result[0].Parent.Nodes.Remove(result[0], eTreeAction.Keyboard);
                    row.Delete();                    
                    FrmMain.MainForm.TreeManipulation.AdapterDependencyTable.Update(row);
                }

        }

        private void cbSolveConflicType_SelectedValueChanged(object sender, EventArgs e)
        {
            txtSolveConflictCommand.Enabled = false;
            if (cbSolveConflicType.SelectedValue != null)
            {
                if (cbSolveConflicType.SelectedValue.ToString() == KeeperSyncUtility.Solve_CallProc)
                {
                    txtSolveConflictCommand.Enabled = true;
                }

            }
        }
    }
}
